package com.huage.courseplatform.framework.api;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huage.courseplatform.framework.annotation.RepeatSubmit;
import com.huage.courseplatform.framework.model.param.PagePARAM;
import com.huage.courseplatform.framework.core.IBaseService;
import com.huage.courseplatform.framework.utils.BeanCopyUtils;
import com.huage.courseplatform.framework.utils.EmptyToNullUtils;
import com.huage.courseplatform.framework.utils.ResultUtils;
import com.huage.courseplatform.satoken.model.vo.UserVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *  基础增删查改操作接口，需要定制的覆盖父类方法即可。
 * @param <P> 参数实体类
 * @param <S> Service
 * @param <E> 实体类
 * @param <D> DTO实体类
 */
@Slf4j
public abstract class BaseController<E,D,P, S extends IBaseService<E>> extends AbstractController<E,D,P> {
    @Autowired
    private S baseService;

    public BaseController() {
    }

    @ApiOperation(value = "添加")
    @PostMapping(value = "/add")
    @RepeatSubmit
    public ResultUtils<D> add(@RequestBody @Validated({Insert.class}) P param) {
        E entity = this.paramToEntity(param);
        baseService.save(entity);
        return ResultUtils.success("添加成功",entityToDto(entity));
    }

    @ApiOperation(value = "更新")
    @PutMapping(value = "/update")
    @RepeatSubmit
    public ResultUtils<D> update(@RequestBody @Validated({Update.class}) P param) {
        E entity = this.paramToEntity(param);
        baseService.saveOrUpdate(entity);
        return ResultUtils.success("更新成功",entityToDto(entity));
    }

    @ApiOperation(value = "根据id删除")
    @DeleteMapping(value = "/delete/{id}")
    public ResultUtils<String> deleteById(@PathVariable String id) {
        baseService.removeById(id);
        return ResultUtils.success("删除成功",id);
    }

    @ApiOperation(value = "根据id集合删除")
    @DeleteMapping(value = "/delete")
    public ResultUtils<List<String>> deleteByIds(@RequestBody List<String> ids) {
        baseService.removeByIds(ids);
        return ResultUtils.success("删除成功",ids);
    }


    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/getById/{id}")
    public ResultUtils<D> getById(@PathVariable String id) {
        return ResultUtils.success(entityToDto(baseService.getById(id)));
    }

    @ApiOperation(value = "分页查询")
    @PostMapping(value = "/page")
    public ResultUtils<Page<D>> getByPage(@Validated @RequestBody PagePARAM pagePARAM) throws IllegalAccessException {
        Page<E> page = new Page<E>(pagePARAM.getPageNum(),pagePARAM.getPageSize());
        QueryWrapper<E> qw =  super.getQueryWrapperByPageParam(pagePARAM);
        this.baseService.page(page,qw);
        return ResultUtils.success(super.entityToDto(page));
    }


    protected S getService() {
        return this.baseService;
    }

}
